//
//  YGFoodWikipediaViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/13.
//

#import "YGFoodWikipediaViewController.h"
#import "WJItemsControlView.h"
#import "YGHomeBLL.h"
#import "YGFoodWikipediaTypeModel.h"
#import "YGFoodWekipediaTableViewCell.h"
#import "YGFoodWikipediaLowerModel.h"
#import "YGFoodWikipediaTableModel.h"

@interface YGFoodWikipediaViewController ()<UITableViewDelegate,UITableViewDataSource>



@property (nonatomic, strong) NSArray <YGFoodWikipediaTypeModel *> *typeArray;           //一级分类
@property (nonatomic, strong) NSArray <YGFoodWikipediaTypeModel *> *lowerArray;          //二级分类
@property (nonatomic, strong) WJItemsControlView *itemView;
@property (nonatomic, assign) NSInteger itemSelectIndex;

@property (nonatomic, strong) WJItemsControlView *lowerItemView;
@property (nonatomic, assign) NSInteger lowerSelectIndex;

@property (nonatomic, strong) NSMutableArray *typeArrayTitle;                  //一级分类标题
@property (nonatomic, strong) NSMutableArray *lowerArrayTitle;                 //二级分类标题
 
@property (nonatomic, strong) UITableView *tabelView;
@property (nonatomic, strong) NSArray *tabelViewList;                   //列表数组




@end

@implementation YGFoodWikipediaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏属性
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xffffff)}];
    
    _typeArray = [NSMutableArray array];
    _lowerArray = [NSMutableArray array];
    _tabelViewList = [NSMutableArray array];
   
   self.typeArrayTitle = [NSMutableArray array];
   self.lowerArrayTitle = [NSMutableArray array];

   [self initView];
   [self initconstraints];
   
   [self requestTypeData];
    
  
}

-(void)initView{
    [self.view addSubview:self.itemView];
    [self.view addSubview:self.lowerItemView];
    [self.view addSubview:self.tabelView];
}

-(void)initconstraints{
    [self.tabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lowerItemView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(self.view.mas_height);
        
     
    }];
}



-(void)requestTypeData{
    WeakSelf(self);
    [YGHomeBLL requestFoodWikipediaTypeInfoSuccess:^(NSArray<YGFoodWikipediaTypeModel *> * _Nonnull model) {
            weakself.typeArray = model;
            for (YGFoodWikipediaTypeModel *dmModel in model) {
                [weakself.typeArrayTitle addObject:dmModel.name];
            }
            weakself.itemView.titleArray = [weakself.typeArrayTitle copy];
            //默认选第一个
            YGFoodWikipediaTypeModel *yypeModel = [weakself.typeArray objectAtIndex:0];
            [weakself requestParentId:yypeModel.id];
        } fail:^(NSError * _Nonnull error) {
            
        }];
    
}





-(void)requestParentId:(NSString *)parentId{
    WeakSelf(self);
    [YGHomeBLL requestFoodWikipediaLowerWithParentId:parentId Success:^(NSArray<YGFoodWikipediaTypeModel *> * _Nonnull model) {
            weakself.lowerArray = model;
            [weakself.lowerArrayTitle removeAllObjects];
            for (YGFoodWikipediaTypeModel *dmModel in model) {
                [weakself.lowerArrayTitle addObject:dmModel.name];
            }
            weakself.lowerItemView.titleArray = [weakself.lowerArrayTitle copy];
            //默认选第一个
            YGFoodWikipediaTypeModel *typeModel = [weakself.lowerArray objectAtIndex:0];
            [weakself requestTableViewWithTypeId:typeModel.id];
        } fail:^(NSError * _Nonnull error) {
            
        }];
}

-(void)requestTableViewWithTypeId:(NSString *)typeId{
    WeakSelf(self);
    [YGHomeBLL requestFoodWikipediaTableWithTypeId:typeId page:1 rows:20 Success:^(YGFoodWikipediaTableModel * _Nonnull model) {
        weakself.tabelViewList = model.pageData;
        [weakself.tabelView reloadData];

        } fail:^(NSError * _Nonnull error) {
            
        }];
}


#pragma delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tabelViewList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        YGFoodWekipediaTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"YGFoodWekipediaTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[YGFoodWekipediaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YGFoodWekipediaTableViewCell"];
    }
    YGFoodWikipediaTableArrayModel *model = [self.tabelViewList objectAtIndex:indexPath.row];
    [cell configureData:model];
        return cell;
}



#pragma setter
- (WJItemsControlView *)itemView{
    if (!_itemView) {
        WJItemsConfig *config = [[WJItemsConfig alloc]init];
        CGFloat width = self.view.width;
        _itemView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, width, 44)];
        _itemView.config = config;
        WeakSelf(self);
      //  [_itemView endMoveToIndex:self.itemSelectIndex];
        _itemView.tapItemWithIndex = ^(NSInteger index, BOOL animation) {
        YGFoodWikipediaTypeModel *model = [weakself.typeArray objectAtIndex:index];
            [weakself requestParentId:model.id];
        };
    }
    return _itemView;
}



- (WJItemsControlView *)lowerItemView{
    if (!_lowerItemView) {
        WJItemsConfig *config = [[WJItemsConfig alloc]init];
        config.textColor = UIColorFromRGB(0x999999);
        config.selectedColor = UIColorFromRGB(0x37C8D7);
        CGFloat width = self.view.width;
        _lowerItemView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0, self.itemView.bottom, width, 44)];
        _lowerItemView.config = config;
       // [_lowerItemView endMoveToIndex:self.lowerSelectIndex];
        WeakSelf(self);
        _lowerItemView.tapItemWithIndex = ^(NSInteger index, BOOL animation) {
        YGFoodWikipediaTypeModel *model = [weakself.lowerArray objectAtIndex:index];
            [weakself requestTableViewWithTypeId:model.id];
        };
    }
    return _lowerItemView;
}

-(UITableView *)tabelView{
    if (!_tabelView) {
        _tabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        [_tabelView registerClass:YGFoodWekipediaTableViewCell.class forCellReuseIdentifier:@"YGFoodWekipediaTableViewCell"];
        
    }
    return _tabelView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
